package com.imoblife.brainwavestus.adapter;

import java.util.ArrayList;

import com.imoblife.brainwavestus.R;
import com.imoblife.brainwavestus.product.Product;
import com.imoblife.brainwavestus.util.BitMapFlyweightFactory;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class LvBaseProductAdapter extends BaseAdapter {
	
	public int mLayoutId;
	public Context mCtx;
	public LayoutInflater mInflater;
	public ArrayList<Product> mProductData;
	public int mRightIconId;
	public int mSaleGroupId;
	public boolean mIsShowSaleGroupIcon = true;
	
	public LvBaseProductAdapter(Context ctx,int layoutId) {
		
		mCtx = ctx;
		mLayoutId = layoutId;
		mProductData = new ArrayList<Product>();
		mInflater = (LayoutInflater) ctx
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public LvBaseProductAdapter(Context ctx,int layoutId, ArrayList<Product> list) {
		
		mCtx = ctx;
		mLayoutId = layoutId;
		mProductData = list;
		mInflater = LayoutInflater.from(mCtx);
	}
	
	public LvBaseProductAdapter setData(ArrayList<Product> data) {
		
		mProductData = data;
		
		return this;
	}
	
	public LvBaseProductAdapter setRightIconId(int resId) {
		
		mRightIconId = resId;
		
		return this;
	}
	
	public LvBaseProductAdapter setSaleGroupIcon(int resId) {
		
		mSaleGroupId = resId;
		
		return this;
	}
	
	public boolean getIsShowSaleGroupIcon() {
			
		return mIsShowSaleGroupIcon;
	}
	
	public void setIsShowSaleGroupIcon(boolean isShow){
		
		mIsShowSaleGroupIcon = isShow;
	}
	
	@Override
	public int getCount() {
		
		return mProductData.size();
	}

	@Override
	public Object getItem(int position) {
		
		return null;
	}

	@Override
	public long getItemId(int position) {
	
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder vh = null;

		if (convertView == null) {

			convertView = mInflater.inflate(mLayoutId, null);
			
			vh = new ViewHolder();

			vh.ivProductIcon = (ImageView) convertView
					.findViewById(R.id.product_icon);

			vh.tvProductName = (TextView) convertView
					.findViewById(R.id.product_name);
			
			vh.ivRightIcon = (ImageView) convertView
					.findViewById(R.id.iv_right_icon);
			
			vh.ivSaleGroupIcon = (ImageView) convertView
					.findViewById(R.id.iv_sale_group_icon);
			
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		if (mProductData == null) {

			return null;
		}
		Product product = mProductData.get(position);

		if (product == null) {

			return null;
		}

		String productName = product.getProductInfo().getName();
		String productTitle = product.getProductInfo().getTitle();
		vh.ivProductIcon.setImageBitmap(BitMapFlyweightFactory
				.getBitMapFlyweightFactory(mCtx.getAssets())
				.getBitMapFlyweight(productName).getBitmap());
		vh.tvProductName.setText(productTitle);
		vh.ivRightIcon.setImageResource(mRightIconId);
		vh.ivSaleGroupIcon.setImageResource(mSaleGroupId);
		
		if(!mIsShowSaleGroupIcon) {
			
			vh.ivSaleGroupIcon.setVisibility(View.GONE);
		}
		
		return convertView;
	}
	
	public class ViewHolder {
		ImageView ivProductIcon;
		TextView tvProductName;
		//the icon show in right of the list view item
		ImageView ivRightIcon;
		
		ImageView ivSaleGroupIcon;
		TextView tvOriginalPrice;
		TextView tvDiscountPrice;
	}
}
